package com.fick.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Query;
import org.hibernate.Session;

import com.fick.hibernate.dao.LabDAO;
import com.fick.hibernate.model.Lab;

public class LabManageAction extends DispatchAction {

	private static final Log log = LogFactory.getLog(LabManageAction.class);
	private LabDAO labdao = new LabDAO();

	public ActionForward labList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int currentpage=0;
		
		if(request.getParameter("currentpage")!=null)
		{
			currentpage=Integer.parseInt(request.getParameter("currentpage"));
		}
		String hql = "from Lab";
		Query query = labdao.getSession().createQuery(hql);
		query.setFirstResult(10*currentpage+0);
		query.setMaxResults(10);
		List lablist = query.list();
		int total=labdao.getTotalCount(Lab.class);
		int pagesize=total%10==0?total/10:total/10+1;
		request.setAttribute("currentpage",currentpage);
		 request.setAttribute("pagesize",pagesize);
		request.setAttribute("lablist", lablist);
		return mapping.findForward("lab_list");

	}

	
	public ActionForward labUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		if(request.getParameter("labid")!=null&&!request.getParameter("labid").equals(""))
		{
			 Lab lab=labdao.findById(Integer.parseInt(request.getParameter("labid")));
				lab.setCode(request.getParameter("code"));
				lab.setIsOpen(request.getParameter("isopen"));
				lab.setProjectName(request.getParameter("projectname"));
				lab.setLessonName(request.getParameter("lessonname"));
				lab.setLabName(request.getParameter("labname"));
				 labdao.getSession().saveOrUpdate(lab);
		}
	
		   
            return labList(mapping, form, request, response);
	}
	
	public ActionForward labAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Lab lab=new Lab();
		lab.setCode(request.getParameter("code"));
		lab.setIsOpen(request.getParameter("isopen"));
		lab.setProjectName(request.getParameter("projectname"));
		lab.setLessonName(request.getParameter("lessonname"));
		lab.setLabName(request.getParameter("labname"));
		labdao.getSession().beginTransaction();
		labdao.save(lab);
		labdao.getSession().getTransaction().commit();
            return labList(mapping, form, request, response);
	}
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		    String labid=request.getParameter("labid");
		    Lab lab=labdao.findById(Integer.parseInt(labid));
		    labdao.getSession().beginTransaction();
		    labdao.delete(lab);
		    labdao.getSession().getTransaction().commit();
            return labList(mapping, form, request, response);
	}

}
